package com.loop.exception;

import java.io.FileNotFoundException;
import java.util.Scanner;


public class ExceptionDemo {
    public static void main(String[] args) {

        try {
            fileType("abc.txt");
        } catch (FileNotFoundException e) {
            System.out.println("文件找不到异常");
        } catch (Exception ex){
            System.out.println("异常处理");
        }
        //getEle(new int[2],2);

        testTryCatch();
    }
    //throwDemo,throw为抛出一个异常
    public static int getEle(int[] arr, int index){
        if (arr==null || arr.length<= 0)
            throw new NullPointerException("arr数组不能为null，且数组元素长度不能小于一");
        if (index < 0 || index > arr.length-1)
            throw new ArrayIndexOutOfBoundsException(index+"索引在数组中不存在");
        System.out.println("结束了");
        return arr[index];
    }

    //throwsDemo, 意思为声明一个异常
    public static void fileType(String fileName) throws Exception {
        if (fileName ==null){
            throw new NullPointerException("文件名不能为空");
        }
        if (!"abc.txt".equals(fileName))
            throw new FileNotFoundException("不是abc.txt文件");
        System.out.println("文件对头");
    }

    //try .... catch test
    public static void testTryCatch(){
        int a =0;
        try {
            a =10/0;
            System.out.println(a);
        } catch (Exception exception) {
            a =10;
        }
    }

    public static int testFinally(int a){
        try {
            a = 0;
            return a;
        }finally {
            a=2;
            //return a;
        }

    }
}
